package com.example.myblog.utils;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

public class CodeAutoGenerator {
    public static void main(String[] args) {
        List<String> tables = new ArrayList<>();
//        tables.add("blog");
//        tables.add("blog_manage");
//        tables.add("blog_message");
//        tables.add("comment");
//        tables.add("follow");
//        tables.add("like");
//        tables.add("message");
//        tables.add("user");
        tables.add("blog_tag");
        tables.add("category");
        tables.add("tag");

        FastAutoGenerator.create("jdbc:mysql://43.142.13.61:3306/my_blog","xiawushuang","yx123")
                .globalConfig(builder -> {
                    builder.author("叶兴")
                            .outputDir(System.getProperty("user.dir")+"\\src\\main\\java")
                            .commentDate("yyyy-MM-dd")
                            .fileOverride();
                })
                .packageConfig(builder -> {
                    builder.parent("com.example.myblog")
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .mapper("mapper")
                            .xml("mapper")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml,System.getProperty("user.dir")+"\\src\\main\\resources\\mapper"));

                })
                .strategyConfig(builder -> {
                    builder.addInclude(tables)
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .entityBuilder()
                            .enableLombok()
                            .enableTableFieldAnnotation()
                            .controllerBuilder()
                            .enableHyphenStyle()
                            .formatFileName("%sController")
                            .enableRestStyle()
                            .mapperBuilder()
                            .enableBaseResultMap()
                            .superClass(BaseMapper.class)
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation()
                            .formatXmlFileName("%sMapper");
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
